package com.yzd.learnjava.concurrency;

import com.yzd.learnjava.util.Print;

public class Joining {
	public static void main(String[] args) {
		Sleeper sleeper = new Sleeper("Sleepy", 1500);
		Sleeper grumpy = new Sleeper("Grumpy", 1500);
		
		Joiner dopey = new Joiner("Dopey", sleeper);
		Joiner doc = new Joiner("Doc", grumpy);
		
		grumpy.interrupt();
	}
}

class Sleeper extends Thread{
	private int duration;
	public Sleeper(String name, int sleepTime) {
		super(name);
		this.duration = duration;
		start();
	}
	
	public void run() {
		try {
			sleep(duration);
		} catch (InterruptedException e) {
			Print.print(getName() + " was interupted. isInterpted():" + isInterrupted());
			e.printStackTrace();
			return;
		}
		
		Print.print(getName() + " has awakened");
	}
}

class Joiner extends Thread{
	private Sleeper sleeper;
	public Joiner(String name , Sleeper sleeper) {
		super(name);
		this.sleeper = sleeper;
		start();
	}
	
	public void run() {
		try {
			sleeper.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
			Print.print("Interupted");
		}
		Print.print(getName() + " join completed");
	}
}
